package com.herench.bs.support;

import com.herench.bs.utils.AuthHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by ThinkPad on 2017/8/9.
 */
@Component
public class VerificationCodeSupport {

    @Autowired
    private CleverClinicContext cleverClinicContext;

    private static ConcurrentHashMap<String, VerificationCodeLifeCycle> verificationCodeCache = new ConcurrentHashMap<>();

    /**
     * 根据手机号获取验证码
     *
     * @param phone 手机号
     * @return 返回执行后的结果信息
     */
    public static VerificationCodeLifeCycle getVerificationCode(String phone) {
        return verificationCodeCache.get(phone);
    }


    public void checkVerificationCode(String channelCode, String phone, String verificationCode) throws Exception {
        Map<String, String> authMap = null;
        authMap = AuthHelper.validateVerificationCode(cleverClinicContext.getContextConfig().getAuthUrl(), channelCode, phone, verificationCode);
        if (authMap != null && authMap.get("code") != null) {
            if (!authMap.get("code").equals("0")) {
                throw new RuntimeException("验证码失效,请重新获取.");
            }
        }
    }
}
